package com.ruoyi.web.controller.ibatis;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.share.domain.HouseRateT;
import com.ruoyi.share.service.IHouseRateTService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 利息分摊Controller
 * 
 * @author lijie
 * @date 2023-08-04
 */
@RestController
@RequestMapping("/ibatis/interestShare")
public class HouseRateTController extends BaseController
{
    @Autowired
    private IHouseRateTService houseRateTService;

    /**
     * 查询利息分摊列表
     */
    @GetMapping("/list")
    public TableDataInfo list(HouseRateT houseRateT)
    {
        startPage();
        List<HouseRateT> list = houseRateTService.selectHouseRateTList(houseRateT);
        if (list.size()>0) {
            if (getDataTableSumJudge(list)) {
                HouseRateT houseVO = new HouseRateT();
                houseVO = houseRateTService.selectHouseRateTSum(houseRateT);
                list.add(houseVO);
            }
        }
        return getDataTable(list);
    }

    /**
     * 导出利息分摊列表
     */
     @Log(title = "利息分摊", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, HouseRateT houseRateT)
    {
        List<HouseRateT> list = houseRateTService.selectHouseRateTList(houseRateT);
        ExcelUtil<HouseRateT> util = new ExcelUtil<HouseRateT>(HouseRateT.class);
        util.exportExcel(response, list, "利息分摊数据");
    }

    /**
     * 获取利息分摊详细信息
     */
    @GetMapping(value = "/{rateId}")
    public AjaxResult getInfo(@PathVariable("rateId") Long rateId)
    {
        return AjaxResult.success(houseRateTService.selectHouseRateTByRateId(rateId));
    }

    @GetMapping(value = "/getRateDetail/{rateDetailId}")
    public AjaxResult getRateDetail(@PathVariable("rateDetailId") Long rateDetailId)
    {
        return AjaxResult.success(houseRateTService.getRateDetail(rateDetailId));
    }

    /**
     * 第一次新增利息分摊
     */
    @Log(title = "利息分摊", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HouseRateT houseRateT)
    {
        HouseRateT houseMaxRateT = houseRateTService.selectHouseMaxRateT();
        if(houseMaxRateT!=null){
            if(houseRateT.getRateDate().before(houseMaxRateT.getRateDate())){
                return AjaxResult.error("当前计息日不允许小于上一计息日");
            }
        }
        int i = houseRateTService.insertHouseRateTFirst(houseRateT);
        if(i==2){
            return AjaxResult.error("计息日不允许小于缴费日期");
        }
        if(i==3){
            return AjaxResult.error("当前缴费日期以前没有缴费数据");
        }
        return toAjax(i);
    }

    /**
     * 第二次以后新增利息分摊
     */
//    @Log(title = "利息分摊", businessType = BusinessType.INSERT)
//    @PostMapping
//    public AjaxResult add(@RequestBody HouseRateT houseRateT)
//    {
//        int i = houseRateTService.insertHouseRateT(houseRateT);
//        if(i==2){
//            return AjaxResult.error("计息日不允许小于缴费日期");
//        }
//        return toAjax(i);
//    }

    /**
     * 修改利息分摊
     */
    @Log(title = "利息分摊", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HouseRateT houseRateT)
    {
        return toAjax(houseRateTService.updateHouseRateT(houseRateT));
    }

    /**
     * 修改备注
     */
    @Log(title = "修改备注", businessType = BusinessType.UPDATE)
    @PutMapping(value="/updateRemarkT")
    public AjaxResult updateRemarkT(@RequestBody HouseRateT houseRateT)
    {
        return toAjax(houseRateTService.updateRemarkT(houseRateT));
    }

    /**
     * 修改备注
     */
    @Log(title = "修改利息", businessType = BusinessType.UPDATE)
    @PutMapping(value="/updateRateDetailT")
    public AjaxResult updateRateDetailT(@RequestBody HouseRateT houseRateT)
    {
        houseRateTService.updateRateDetailT(houseRateT);
        houseRateTService.updateHouseRateTByHouseId(houseRateT);
        houseRateTService.updateHouseBalanceSumByHouseId(houseRateT);
        houseRateTService.updateRateSumByRateId(houseRateT);
        houseRateTService.updateRateSubMoney(houseRateT);
        return toAjax(1);
    }

    /**
     * 删除利息分摊
     */
    @Log(title = "利息分摊", businessType = BusinessType.DELETE)
	@DeleteMapping("/{rateIds}")
    public AjaxResult remove(@PathVariable Integer[] rateIds)
    {
        return toAjax(houseRateTService.deleteHouseRateTByRateIds(rateIds));
    }

    @GetMapping(value = "/listRateDetail")
    public TableDataInfo listRateDetail(HouseRateT houseRateT)
    {
        startPage();
        List<HouseRateT> list = houseRateTService.selectHouseRateDetailTByRateId(houseRateT);
        return getDataTable(list);
    }

}
